package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.statistic.UserVo;
import com.ruoyi.system.domain.statistic.UserVo2;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.StatisticService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class StatisticServiceImpl implements StatisticService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysPostMapper sysPostMapper;


    /**
     * 统计各部门的用户人数
     * @return
     */
    @Override
    public UserVo countUserByPost() {
        List<String> Post=new ArrayList<>();
        List<Integer> count=new ArrayList<>();

        List<SysPost> sysPosts = sysPostMapper.selectPostAll();

        List<String> PostNames=new ArrayList<>();

        for (SysPost s:sysPosts){
            PostNames.add(s.getPostName());
        }

        for (String s:PostNames){

            int num = sysPostMapper.countByPostName(s);

            Post.add(s);
            count.add(num);
        }

        UserVo userVo=new UserVo(Post,count);

        return userVo;
    }

    @Override
    public List<UserVo2> countUserByRole() {
        List<UserVo2> list=new ArrayList<>();

        List<SysRole> roles = sysRoleMapper.selectRoleAll();

        List<String> RoleNames=new ArrayList<>();

        for (SysRole s:roles){
            RoleNames.add(s.getRoleName());
        }

        for (String s:RoleNames){
            Integer num = sysRoleMapper.countByRoleName(s);
            list.add(new UserVo2(num,s));
        }

        return list;
    }
}
